package com.yulan.Array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 39. 组合总和
 */
public class LeetCode_39 {

    List<List<Integer>> ans = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        if(candidates == null || candidates.length == 0 || target < 0)
            return ans;
        Arrays.sort(candidates);

        List<Integer> list = new ArrayList<>();
        dfs(0,candidates,target,list);
        return ans;
    }

    public void dfs(int start,int[] candidates,int target,List<Integer> list){
        if(target<0)
            return;
        if(target==0)
            ans.add(new ArrayList<>(list));

        for(int i = start;i<candidates.length;i++){
            list.add(candidates[i]);

            dfs(i,candidates,target-candidates[i],list);

            list.remove(list.size()-1);
        }
    }

    @Test
    public void test1(){
        int[] arr = {2,3,6,7};
        for(List<Integer> list : combinationSum(arr,7)){
            System.out.println(list);
        }
    }
}
